//
//  SettingAboutVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/5.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "SettingAboutVC.h"
#import <StoreKit/StoreKit.h>
#import "ImportSelectView.h"
#import "SettingUpdateExplainVC.h"
#import "SettingWeiXinVC.h"
#import "WebVC.h"

@interface SettingAboutVC ()

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) ImportSelectView *imprintView;
@property (nonatomic, strong) ImportSelectView *gradeView;
@property (nonatomic, strong) ImportSelectView *weiXinView;
@property (nonatomic, strong) ImportSelectView *serviceView;

@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation SettingAboutVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
}
- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)initUI {
    
    self.title = @"关于";
    
    self.mainScrollView.backgroundColor = ListMaoBackgroundColor;
    if ([[Mediator sharedInstance] Login_IsShowFourModule]) {
        self.imprintView.hidden = NO;
        [self.imprintView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.headerView.mas_bottom);
            make.height.mas_equalTo(55);
        }];
        [self.gradeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.imprintView.mas_bottom);
            make.height.mas_equalTo(55);
        }];
    }else {
        self.imprintView.hidden = YES;
        [self.gradeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.headerView.mas_bottom);
            make.height.mas_equalTo(55);
        }];
    }
    
    [self.weiXinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.gradeView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    [self.serviceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.weiXinView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    
    [self.view bringSubviewToFront:self.bottomLabel];
}



- (void)clickAction:(UIButton *)sender {
    
    switch (sender.tag) {
        case 1:
        {
            SettingUpdateExplainVC *vc = [[SettingUpdateExplainVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            [self gotoPingFen];
        }
            break;
        case 3:
        {
            SettingWeiXinVC *vc = [[SettingWeiXinVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            WebVC *vc = [[WebVC alloc] init];
            vc.urlString = @"https://www.baidu.com";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)gotoPingFen {
    
    SKStoreProductViewController *storeProductViewController = [[SKStoreProductViewController alloc] init];
    [storeProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:@""} completionBlock:^(BOOL result, NSError * _Nullable error) {
        
        if (error)
            NSLog(@"error %@ with userInfo %@",error,[error userInfo]);
        else
            [self presentViewController:storeProductViewController animated:YES completion:nil];
    }];
    
    
}
//Appstore 取消按钮的回调
-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao, 400);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}

- (UIView *)headerView {
    
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 182)];
        _headerView.backgroundColor = ListMaoBackgroundColor;
        
        [self.mainScrollView addSubview:_headerView];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nameLabel nameLabel"]];
        [headerImageView setContentMode:UIViewContentModeScaleAspectFit];
        [_headerView addSubview:headerImageView];
        [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(60, 60));
            make.centerX.mas_equalTo(_headerView);
            make.top.mas_equalTo(_headerView.mas_top).offset(35);
        }];
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text =  [infoDictionary objectForKey:@"CFBundleDisplayName"];
        [nameLabel setTextAlignment:NSTextAlignmentCenter];
        nameLabel.textColor = HexColor(0x333333);
        nameLabel.font = [UIFont systemFontOfSize:16];
        [_headerView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_headerView);
            make.top.mas_equalTo(headerImageView.mas_bottom).offset(15);
        }];
        
        UILabel *versionsLabel = [[UILabel alloc] init];
        versionsLabel.text = [NSString stringWithFormat:@"v%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]];
        [versionsLabel setTextAlignment:NSTextAlignmentCenter];
        versionsLabel.textColor = HexColor(0xaaaaaa);
        versionsLabel.font = [UIFont systemFontOfSize:13];
        [_headerView addSubview:versionsLabel];
        [versionsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_headerView);
            make.top.mas_equalTo(nameLabel.mas_bottom).offset(4);
        }];
        
    }
    return _headerView;
}

- (ImportSelectView *)imprintView {
    
    if (!_imprintView) {
        _imprintView = [[ImportSelectView alloc] init];
        _imprintView.titleLabel.text = @"版本说明";
        _imprintView.rightLabel.text = @"";
        [self.mainScrollView addSubview:_imprintView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 1;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_imprintView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_imprintView);
            make.left.mas_equalTo(_imprintView.titleLabel.mas_right);
        }];
        [_imprintView.lineImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_imprintView.mas_right);
        }];
    }
    return _imprintView;
}
- (ImportSelectView *)gradeView {
    
    if (!_gradeView) {
        _gradeView = [[ImportSelectView alloc] init];
        _gradeView.titleLabel.text = @"去评分";
        _gradeView.rightLabel.text = @"";
        [self.mainScrollView addSubview:_gradeView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 2;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_gradeView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_gradeView);
            make.left.mas_equalTo(_gradeView.titleLabel.mas_right);
        }];
        [_gradeView.lineImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_gradeView.mas_right);
        }];
    }
    return _gradeView;
}
- (ImportSelectView *)weiXinView {
    
    if (!_weiXinView) {
        _weiXinView = [[ImportSelectView alloc] init];
        _weiXinView.titleLabel.text = @"关注微信";
        _weiXinView.rightLabel.text = @"";
        [self.mainScrollView addSubview:_weiXinView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 3;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_weiXinView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_weiXinView);
            make.left.mas_equalTo(_weiXinView.titleLabel.mas_right);
        }];
        [_weiXinView.lineImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_weiXinView.mas_right);
        }];
    }
    return _weiXinView;
}
- (ImportSelectView *)serviceView {
    
    if (!_serviceView) {
        _serviceView = [[ImportSelectView alloc] init];
        _serviceView.titleLabel.text = @"服务协议";
        _serviceView.lineImageView.hidden = YES;
        _serviceView.rightLabel.text = @"";
        
        [self.mainScrollView addSubview:_serviceView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 4;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_serviceView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_serviceView);
            make.left.mas_equalTo(_serviceView.titleLabel.mas_right);
        }];
        
    }
    return _serviceView;
}

- (UILabel *)bottomLabel {
    
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenHeight - SafeAreaTopHeight - 50, ScreenWidthMao, 20)];
        _bottomLabel.text = @"@ Copyright 2018 爱尚金服";
        _bottomLabel.font = [UIFont systemFontOfSize:10];
        _bottomLabel.textColor = HexColor(0xaaaaaa);
        [_bottomLabel setTextAlignment:NSTextAlignmentCenter];
        
        [self.view addSubview:_bottomLabel];
    }
    return _bottomLabel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
